<?php
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MVC.
 *
 * PHP-MVC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MVC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MVC; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-mvc/view
 * @subpackage  form
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

/**
 * Zend class for extension
 */
require_once 'mvc/view/helper/zend/FormElement.php';


/**
 * Helper to generate a "label" element
 * 
 * @category   Melange
 * @package    php-mvc
 * @subpackage view
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (jeroen@melange.nl)
 * @license    http://www.gnu.org/licenses/licenses.html#GPL    The GNU General Public License
 */
class PHP_MVC_View_Form_Helper_Label extends Zend_View_Helper_FormElement {

    /**
     * Generates a 'label' element.
     * 
     * @access public
     * 
     * @param string|array $name If a string, the element name.  If an
     * array, all other parameters are ignored, and the array elements
     * are extracted in place of added parameters.
     * 
     * @param mixed $value The element value.
     * 
     * @param array $attribs Attributes for the element tag.
     * 
     * @return string The element XHTML.
     */
    public function label($name, $value, $required=false, $attribs = null, $escape=false)
    {
        $info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, id, value, attribs, options, listsep, disable

        // Override the class if error (Made global in ActionController) exists for this field.
        $actionErrors = $this->pageContext->request->getAttribute(MVC::ERROR_KEY);
        if(!is_null($actionErrors) && !$actionErrors->isEmpty() && !is_null($actionErrors->get($name))) {
            $attribs["class"] = "error";
        }

        // build the element
        if ($disable) {

            // disabled. no hidden value because it can't be clicked.
            $xhtml = '[' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . ']';

        } else {

            // enabled
            $xhtml = '<label '
                   . ' for="' . htmlspecialchars($name, ENT_COMPAT, 'UTF-8') . '"';

            // add attributes and close
            $xhtml .= $this->_htmlAttribs($attribs) . ' />';

            if($required)
                $xhtml .= "<em>*</em> ";

            $xhtml .= $escape ? htmlspecialchars($value, ENT_COMPAT, 'UTF-8') : $value;

            $xhtml .=  '</label>';
        }

        return $xhtml;
    }
}
?>